import { RabbitMQClient } from '@roasmax/rabbitmq';
import { RedisToolkit } from '@roasmax/redis';
import dotenv from 'dotenv';
import { createOnWeiboLoginSuccess } from './works/createOnWeiboLoginSuccess';
import { createOnWeiboDetail } from './works/createOnWeiboDetail';

dotenv.config();

async function main() {
  // Initialize Redis client
  const redis = new RedisToolkit({
    host: process.env['REDIS_HOST'] || 'localhost',
    port: parseInt(process.env['REDIS_PORT'] || '6379'),
    password: process.env['REDIS_PASSWORD'],
    autoSerialize: true,
  });
  await redis.connect();

  // Initialize RabbitMQ client
  const rabbitmq = RabbitMQClient.create({
    host: process.env['RABBITMQ_HOST'] || 'localhost',
    port: parseInt(process.env['RABBITMQ_PORT'] || '5672'),
    username: process.env['RABBITMQ_USERNAME'] || 'guest',
    password: process.env['RABBITMQ_PASSWORD'] || 'guest',
  });
  await rabbitmq.initialize();
  // weibo_login_success
  const weiboLoginSuccessWorkQueue = rabbitmq.createWorkQueue('weibo_login_success');
  await weiboLoginSuccessWorkQueue.setupQueue();
  await weiboLoginSuccessWorkQueue.startWorkers(createOnWeiboLoginSuccess(redis), 3);
  // weibo_detail
  const weiboDetailWorkQueue = rabbitmq.createWorkQueue(`weibo_detail`)
  await weiboDetailWorkQueue.setupQueue();
  await weiboDetailWorkQueue.startWorkers(createOnWeiboDetail(redis, rabbitmq), 1)
  console.log('Subscriber is listening for messages on weibo_login_success...');
}

main().catch(console.error);

